package com.newupbank.schedule.config;

import com.newupbank.schedule.mail.dao.ICronDao;
import com.newupbank.schedule.mail.vo.CronVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/**
 * @author: liminghui
 * @date: 2021/1/17 23:09
 * @version: 1.0
 * @description: 从数据库读取发送邮件配置
 */
@Configuration
public class MailConfiguration {
    @Autowired
    private ICronDao cronDao;

    @Bean(value = "mailCronVO")
    public CronVO initMailCronVO() {
        // 发送邮件的配置读取到properties中
        CronVO cronVO = cronDao.queryCronVOByType("mail");
        if (cronVO != null && cronVO.getCron() != null) {
            System.setProperty("mailTaskCron", cronVO.getCron());
        }
        return cronVO;
    }

    @Bean(value = "domainCronVO")
    public CronVO initDomainCronVO() {
        // 发送邮件的配置读取到properties中
        CronVO cronVO = cronDao.queryCronVOByType("domain");
        if (cronVO != null && cronVO.getCron() != null) {
            System.setProperty("domainTaskCron", cronVO.getCron());
        }
        return cronVO;
    }

    @Bean
    public TaskScheduler scheduledExecutorService() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(8);
        scheduler.setThreadNamePrefix("scheduled-thread-");
        return scheduler;
    }


}
